<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/4/16
 * Time: 14:34
 */

namespace app\mobile\controller\user;


class Account extends Common
{
    private $mod;
    public function _initialize()
    {
        parent::_initialize();
        $this->mod = model('user');
        $this->_name = 'User';
    }
    public function info(){
        $info = $this->mod->where(['id'=>$this->userInfo['id']])->find();
        $this->assign('userInfo',$info);
        $this->assign('title','基本资料');
        return $this->fetch();
    }
    public function bind(){
        $info = $this->mod->where(['id'=>$this->userInfo['id']])->field('qq_openid,sina_openid')->find();
        $this->assign('userInfo',$info);
        return $this->fetch();
    }
    public function suggest(){
        $info = $this->mod->where(['id'=>$this->userInfo['id']])->field('username,mobile,email')->find();
        $this->assign('userInfo',$info);
        return $this->fetch();
    }
    public function suggestDo(){
        $this->_name = 'Suggest';
        \app\common\model\Suggest::event('before_insert',function($obj){
            $obj->user_id = $this->userInfo['id'];
        });
        parent::addDo();
    }
    public function suggestDel(){
        $this->_name = 'Suggest';
        parent::delete();
    }
    public function suggestList(){
        $where['user_id'] = $this->userInfo['id'];
        $lists = model('suggest')->where($where)->paginate(15);
        $this->assign('lists',$lists);
        $this->assign('pages',$lists->render());
        return $this->fetch();
    }
    //编辑 前置事件
    public function infoDo(){
        if(request()->isPost()){
            $data = input('post.');
            if(!isset($data['id']) || empty($data['id'])){
                request()->isAjax() ? $this->ajaxReturn(0,'参数错误') : $this->error('参数错误');
            }
            $img = $this->_uploadImg();
            if($img){
                $data['headimgurl'] = $img;
            }
            //allowField 过滤非数据表字段
            $base = $this->mod->allowField(true)->save($data,['id'=>$data['id']]);
            $ext = $this->mod->userInfo->save($data['data']);
            if($base || $ext ){
                request()->isAjax() ? $this->ajaxReturn(1,'编辑成功','','edit') : $this->success('编辑成功');
            }else{
                request()->isAjax() ? $this->ajaxReturn(0,'编辑失败','') : $this->error('编辑失败');
            }
        }
    }
    public function password(){
        $this->assign('title','修改密码');
        return $this->fetch();
    }
    public function passwordDo(){
        $oldpwd = input('post.oldPass');
        $newpwd = input('post.newPass');
        $newpwd2 = input('post.newPass2');
        if(strlen($oldpwd)<6){
            $this->error('密码至少6位数');
        }elseif($newpwd!=$newpwd2){
            $this->error('两次密码输入不一致');
        }else{
            $where['id'] = $this->userInfo['id'];
            $where['password'] = passwordEncode($oldpwd);
            $count = $this->mod->where($where)->count();
            if($count == 0){
                $this->error('原密码不正确');
            }else{
                if($this->mod->save(['password'=>$newpwd],$where)){
                    $this->error('密码修改成功');
                }else{
                    $this->error('密码修改失败');
                }
            }
        }
    }
}